﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WebApplication5
{
    public class SqlHelper
    {
        public string url = "server=.;uid=sa;pwd=123456;database=Student_db";
        public SqlConnection con = null;


        public SqlHelper()
        {
            con = new SqlConnection(url);
        }

        public DataTable Get(string sql, SqlParameter[] pars)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand com = new SqlCommand(sql, con);
                if (pars != null)
                {
                    com.Parameters.AddRange(pars);
                }
                SqlDataAdapter sda = new SqlDataAdapter(com);
                DataSet ds = new DataSet();

                sda.Fill(ds);
                return ds.Tables[0];

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
        public bool Execute(string sql, SqlParameter[] pars)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand com = new SqlCommand(sql, con);
                if (pars != null)
                {
                    com.Parameters.AddRange(pars);
                }
                int result = com.ExecuteNonQuery();

                return result > 0 ? true : false;

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
    }
}